<?php

App::uses('AppController', 'Controller');

class GuestsController extends AppController {
	
	public $uses = array('Guest');
	
	public $paginate = array(
		'limit' => ITEMS_PER_PAGE,
		'order' => array(
			'Guest.name'=>'asc'
		)
	);
	
	public function index() {
		$this->set("guests", $this->paginate('Guest'));
	}

	private function _setCommonData() {
		
	}
	
	public function create() {
		$this->_setCommonData();
		$this->set('data', $this->Guest->create());
		$this->render('form');
	}
	
	public function edit($id) {
		$this->_setCommonData();
		$this->set('data', $this->Guest->findById($id));
		$this->render('form');
	}
	
	public function save() {
		$this->_setCommonData();
		
		if($this->request->is('post')) {
			$data = $this->data;
			$this->Guest->create($data);
			
			if(! $this->Guest->validates()) {
			    $this->setFlash(strtotitle(__('save failed')) . ". " . strtotitle(__('invalid data')) . ".");
			    $this->_setCommonData();
			    $this->set("data", $this->data);
			    return $this->render('form');
			}
			
			if(! $this->Guest->save($data)) {
			    $this->setFlash(strtotitle(__('save failed')) . ". " . strtotitle(__('invalid data')) . ".");
			    $this->set("data", $this->data);
			    $this->_setCommonData();
			    return $this->render('form');
			}
			
			$this->setFlash(strtotitle(__('saved')) . ".", "ok");
			$this->redirect(array('action'=>'edit', $this->Guest->id));			
		}
		$this->redirect(array('action'=>'index'));
	}
	
	public function delete($id) {
		$this->Guest->delete($id);
		$this->redirect(array('action'=>'index'));
	}
	
}

?>